BI.namespace('core');

/**
 * 
 */
BI.core.Renderer = new Class({
	initialize : function (context, scene, camera, eventManager) {
		this.context = context;
		this.scene = scene;
    	this.camera = camera;  
    	this.eventManager = eventManager;
    	this.rendererContext = new BI.core.RendererContext(context, scene);
	},
    update: function() {
    	// clear view 
    	this.context.clearRect(0, 0, this.camera.width, this.camera.height);
    	// prepare context object that will be passed to drawable elements
    	this.rendererContext.viewPort = this.camera.getViewPort();
    	// check if there is background to draw  
    	if (!!this.scene.background){
			this.scene.background.draw(this.rendererContext);
		}
    	// draw layers
    	if (!!this.scene.layers){
    		for (var layerIndex = 0; layerIndex < this.scene.layers.length; layerIndex++){
    			var elements = this.scene.layers[layerIndex].sprites;
    			
    			for (var elementIndex = 0; elementIndex < elements.length; elementIndex++){
    				var element = elements[elementIndex];
    				element.draw(this.rendererContext);
    				this.eventManager.collectEvents(element, this.rendererContext);
    			}
    		}
    	}
    	// fire collected events
    	this.eventManager.fireEvents();
    }
});

/**
 * 
 */
BI.core.RendererContext = new Class({
	initialize: function (drawContext, scene){
		this.drawContext = drawContext;
		this.sceneDimension = {width: scene.width, height: scene.height};
    	this.viewPort = null;
    	this.clickEvent = null;
    	this.mouseOverEvent = null;
	}
});

